package com.yurun.loginmodule.controller;

import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.*;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orgstudentmodule.service.TrainingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import javax.validation.Valid;
import java.math.BigInteger;
import java.util.Date;

@Api(tags = "训练管理")
@RestController
@RequestMapping("/trainingManage")
public class TrainingController {

    @Autowired
    private TrainingService trainingService;


    @ApiOperation(value = "根据条件查询训练表")
    //@RequiresPermissions("/allTrainingInfo")
    @RequestMapping("/allTrainingInfo")
    public CommonResult allTrainingInfo(ServletRequest request,BigInteger employeeId, BigInteger studentId, BigInteger classroomId,Integer ifAlone, String tag, BigInteger branchId, String keyword, String recordMonth, String recordYear, String lessonName, String studentName, int pageNum, int pageSize) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取员工id
        //String employeeId = JWTUtil.getUsername(token);
        System.out.println(tag);
        return trainingService.allTrainingInfo(studentId, classroomId, tag, ifAlone, BigInteger.valueOf(Long.parseLong(organizationId)), branchId,  employeeId, keyword, recordMonth, recordYear, lessonName, studentName, pageNum, pageSize);
    }

    @ApiOperation(value = "获取某一老师学生训练信息")
    //@RequiresPermissions("/teacherTrainingInfo")
    @RequestMapping("/teacherTrainingInfo")
    public CommonResult getOneTeacherStudentTraining(String recordMonth, String recordYear,String tag, String keyword, BigInteger teacherId, int pageNum, int pageSize){

        return trainingService.getOneTeacherStudentTraining(recordMonth,recordYear, tag, keyword, teacherId, pageNum, pageSize);
    }

    @ApiOperation(value = "增加学生训练信息")
    //@RequiresPermissions("/addStudentTrainingTable")
    @RequestMapping("/addStudentTrainingTable")
    public CommonResult addStudentTrainingTable(ServletRequest request, @Valid @RequestBody StudentTrainingTable studentTrainingTable) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);

        studentTrainingTable.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        studentTrainingTable.setEmployeeId(BigInteger.valueOf(Long.parseLong(employeeId)));
        studentTrainingTable.setCreateTime(new Date());
        studentTrainingTable.setUpdateTime(new Date());
        studentTrainingTable.setIsDel(0);
        return trainingService.addStudentTrainingTable(BigInteger.valueOf(Long.parseLong(employeeId)), studentTrainingTable, studentTrainingTable.getIfAlone());
    }

    @ApiOperation(value = "所有学生列表")
    @RequestMapping("/studentList")
    public CommonResult studentList(ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        return trainingService.studentList(BigInteger.valueOf(Long.parseLong(employeeId)), BigInteger.valueOf(Long.parseLong(organizationId)), BigInteger.valueOf(Long.parseLong(branchId)));
    }

    @ApiOperation(value = "获取某一个表的训练信息")
    //@RequiresPermissions("/getOneTrainingInfo")
    @RequestMapping("/getOneTrainingInfo")
    public CommonResult getOneTrainingInfo(StudentTrainingTable studentTrainingTable){
        return trainingService.getOneTrainingInfo(studentTrainingTable);
    }

    @ApiOperation(value = "删除训练信息")
    //@RequiresPermissions("/delTraining")
    @RequestMapping("/delTraining")
    public CommonResult delTraining(ServletRequest request, StudentTrainingTable studentTrainingTable) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        studentTrainingTable.setUpdateTime(new Date());
        System.out.println("这是控制");
        return trainingService.delTraining(BigInteger.valueOf(Long.parseLong(employeeId)), studentTrainingTable, studentTrainingTable.getIfAlone());
    }

    @ApiOperation(value = "修改训练信息")
    //@RequiresPermissions("/editTraining")
    @RequestMapping("/editTraining")
    public CommonResult editTraining(ServletRequest request,@RequestBody StudentTrainingTable studentTrainingTable) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        return trainingService.editTraining(BigInteger.valueOf(Long.parseLong(employeeId)), studentTrainingTable, studentTrainingTable.getIfAlone());
    }

    @ApiOperation(value = "获取所有教师名")
    @RequestMapping("/getTeacherOption")
    public CommonResult getTeacherOption(ServletRequest request) {
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);
        // 获取员工id
        String employeeId = JWTUtil.getUsername(token);
        // 获取分店id
        String branchId = JWTUtil.getBranchId(token);
        StudentTrainingTable studentTrainingTable = new StudentTrainingTable();
        studentTrainingTable.setOrganizationId(BigInteger.valueOf(Long.parseLong(organizationId)));
        studentTrainingTable.setEmployeeId(BigInteger.valueOf(Long.parseLong(employeeId)));
        studentTrainingTable.setBranchId(BigInteger.valueOf(Long.parseLong(branchId)));
        return trainingService.getTeacherOption(studentTrainingTable);
    }

}
